package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.model.vo.DeviceHistoryVO;
import com.yupi.springbootinit.model.vo.DeviceOverviewItemVO;
import com.yupi.springbootinit.model.vo.DeviceStatusVO;
import com.yupi.springbootinit.service.DeviceDataService;
import com.yupi.springbootinit.service.StatisticsService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/devices")
public class DevicesController {

    private final DeviceDataService deviceDataService;
    private final StatisticsService statisticsService;


    public DevicesController(DeviceDataService deviceDataService,
                             StatisticsService statisticsService) {
        this.deviceDataService = deviceDataService;
        this.statisticsService = statisticsService;
    }


    @GetMapping("/status")
    public BaseResponse<List<DeviceOverviewItemVO>> getOverview() {
        return ResultUtils.success(deviceDataService.getOverview());
    }


    @GetMapping("/{deviceId}/status")
    public BaseResponse<DeviceStatusVO> getStatus(@PathVariable Integer deviceId) {
        DeviceStatusVO vo = deviceDataService.getLatestStatus(deviceId);
        return ResultUtils.success(vo);
    }

    @GetMapping("/{deviceId}/history")
    public BaseResponse<DeviceHistoryVO> getHistory(@PathVariable Integer deviceId,
                                                   @RequestParam String fields,
                                                   @RequestParam(defaultValue = "-24h") String start,
                                                   @RequestParam(defaultValue = "1m") String agg) {
        DeviceHistoryVO vo = deviceDataService.getHistory(deviceId, fields, start, agg);
        return ResultUtils.success(vo);
    }
    //相对时间稼动率
    @GetMapping("/{deviceId}/availability")
    public BaseResponse<Double> getAvailability(@PathVariable Integer deviceId,
                                                @RequestParam(defaultValue = "-24h") String range,
                                                @RequestParam(defaultValue = "true") boolean includeBusy) {
        double val = statisticsService.calcAvailability(deviceId, range, includeBusy);
        return ResultUtils.success(val);
    }
}